<!-- 医生就诊预约界面: 转诊和接收 -->
<template>
  <div class="MyContainer">
    <div class="mt-4">
      <el-input style="width: 300px;" v-model="input_name" placeholder="请输入姓名" class="input-with-select"></el-input>
      <el-button type="primary" :icon="Search" style="margin-left:10px" @click="query()">查询</el-button>
    </div>
    <el-table :data="tableData" style="margin-top:25px" header-cell-class-name="table-header">
      <el-table-column prop="appReqId" label="ID" width="60" />
      <el-table-column prop="cusName" label="姓名" width="120" />
      <el-table-column prop="cusGender" label="性别" width="100" />
      <el-table-column prop="appReqTime" label="预约时间" width="200" />
      <el-table-column prop="appReqIllness" label="病情简介" />
      <el-table-column fixed="right" label="处理操作" width="200">
        <template #default="scope">
          <el-button link type="primary"  @click="Click11(scope.row.appReqId,scope.$index);dialogTableVisible = true">转诊</el-button>
          <el-button link type="primary"  @click="Click2(scope.$index)">接收</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogTableVisible" title="请选择转诊医生">
      <el-table :data="gridData">
        <el-table-column prop="docId" label="编号" width="80" />
        <el-table-column prop="docName" label="医生姓名"  />
        <el-table-column prop="docHospital" label="所属医院"  />
        <el-table-column prop="docDepartment" label="科室"  />
        <el-table-column fixed="right" label="处理操作">
          <template #default="scope">
            <el-button link type="primary"  @click="Click1(scope.$index);dialogTableVisible = false" size="large">转诊</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="contracted-user">
import { reactive, ref } from 'vue';
import { Search} from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getDocVisitInfo,getAlldoctorInfo,acceptReqId,transferReqId } from '../api/manager';

const input_name = ref('')

interface TableItem{
  appReqId:number
  cusName:string
  cusGender:string
  appReqTime:string
  appReqIllness:string
  status:number
}

interface TableItem_2{
  docId:number
  docName:string
  docHospital:string
  docDepartment:string
}

const tableData = ref<TableItem[]>([])
const gridData = ref<TableItem_2[]>([])

const getData = () =>{
//tableData
getDocVisitInfo('').then(res =>{
  if(res.code == 200){
    //console.log('tableData',res)
    tableData.value = res.data
  }
}).catch(err => {console.log('请求失败',err)})

//gridData
getAlldoctorInfo().then(res=>{
  if(res.code==200){
    //console.log('gridData',res)
    gridData.value=res.data    
   }
}).catch(err => {console.log('请求失败',err)})
}
getData();

const now = new Date()
const dialogTableVisible = ref(false)

//查询
const query = () => {
const cusName=input_name.value
getDocVisitInfo(cusName).then(res =>{
  if(res.code == 200){
    tableData.value = res.data
  }
}).catch(err => {console.log('请求失败',err)})
}

const form = reactive({
    appReqId:0,
    docId:0,
})

const Click11 = (id:number,index1:number) => {
  form.appReqId=id
  console.log("form",form)
}

//转诊
const Click1 = (index) => {
  ElMessageBox.confirm(
    '请确定是否将患者转诊',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    console.log("用户申请id:",form.appReqId)
    form.docId=gridData.value[index].docId
    console.log("医生id:",form.docId)
    transferReqId(form.appReqId,form.docId).then(res=>{
      console.log("res:",res)
      ElMessage({
        type: 'success',
        message: res.msg,
      })
      query()
    }).catch(err=>{console.log("fail")})
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '取消操作',
    })
  })
}

const Click2 = (index) => {
ElMessageBox.confirm(
  '请确定是否接收患者',
  '提示',
  {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }
).then(() => {
  ElMessage({
      type: 'success',
      message: '操作成功',
  })
  acceptReqId(tableData.value[index].appReqId).then(res=>{
    query()
  }).catch(err=>{console.log("fail")})
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '取消操作',
    })
  })
}

</script>

<style scoped>
.mt-4 {
  float: left;
  margin-bottom: 40px;
  margin-top: 30px;
}

</style>
